export default {
  install (Vue, router, ACCESS_TOKEN) {
    let token = ''
    router.beforeEach((to, from, next) => {
      if (to.query.token) { // 有token获取接口
        token = to.query.token
        const originAppId = to.query.originAppId
        window.$postType(window.$api.upms.upms.loginByToken, { token, originAppId }).then(res => {
          Vue.ls.set(ACCESS_TOKEN, res.data)
          router.push({ query: {} })
          next()
        })
      } else { // 没token判断是否hash模式
        if (window.location.hash) {
          if (window.location.search.includes('token')) {
            const href = window.location.origin + '/' + window.location.hash + (window.location.hash.includes('?') ? decodeURIComponent(window.location.search).replace('?', '&') : window.location.search)
            window.location.href = href
          } else {
            next()
          }
        } else {
          next()
        }
      }
    })
  }
}
